//
//  EconomicCalendarTableViewCell.m
//  BondWatch
//
//  Created by Jason Yuan on 11/30/15.
//  Copyright © 2015 Jason Yuan. All rights reserved.
//

#import "EconomicCalendarTableViewCell.h"

@implementation EconomicCalendarTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//        [self setBackgroundColor:[UIColor colorWithRed:239.0/255.0 green:146.0/255.0 blue:0.0 alpha:1.0]];
        
        dateTimeLabel = [[UILabel alloc] init];
        dateTimeLabel.textAlignment = NSTextAlignmentLeft;
        dateTimeLabel.font = ARIAL10;
        
        eventLabel = [[UILabel alloc] init];
        eventLabel.textAlignment = NSTextAlignmentLeft;
        eventLabel.font = ARIALBOLD10;
        
        periodLabel = [[UILabel alloc] init];
        periodLabel.textAlignment = NSTextAlignmentLeft;
        periodLabel.font = ARIAL10;
        
        surveyLabel = [[UILabel alloc] init];
        surveyLabel.textAlignment = NSTextAlignmentLeft;
        surveyLabel.font = ARIAL10;
        
        actualLabel = [[UILabel alloc] init];
        actualLabel.textAlignment = NSTextAlignmentLeft;
        actualLabel.font = ARIAL10;
        
        proirLabel = [[UILabel alloc] init];
        proirLabel.textAlignment = NSTextAlignmentLeft;
        proirLabel.font = ARIAL10;
        
        revisedLabel = [[UILabel alloc] init];
        revisedLabel.textAlignment = NSTextAlignmentLeft;
        revisedLabel.font = ARIAL10;
        
        [self addSubview:dateTimeLabel];
        [self addSubview:eventLabel];
        [self addSubview:periodLabel];
        [self addSubview:surveyLabel];
        [self addSubview:actualLabel];
        [self addSubview:proirLabel];
        [self addSubview:revisedLabel];
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setUIFrame {
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeLeft ||
        [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeRight) {
        [dateTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(2);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [eventLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(dateTimeLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(160);
        }];
        
        [periodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(eventLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
        
        [surveyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(periodLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
        
        [actualLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(surveyLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
        
        [proirLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(actualLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
        
        [revisedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(proirLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
    } else {
        [dateTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(2);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [eventLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(dateTimeLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(160);
        }];
        
        [periodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(eventLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
        
        [surveyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(periodLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
        
        [actualLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(surveyLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
        
        [proirLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(actualLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
        
        [revisedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(proirLabel.mas_right).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(40);
        }];
    }
}

- (void)setLabelText:(NSDictionary *)data {
    NSString *dateTime = [data objectForKey:@"dateTime"];
    dateTimeLabel.text = dateTime;
    
    NSString *today = [DateUtil todayStringFromDate];
    NSString *dataDateTime = [dateTime substringToIndex:10];
    
    if ([today isEqualToString:dataDateTime]) {
        [self setBackgroundColor:[UIColor colorWithRed:239.0/255.0 green:146.0/255.0 blue:0.0 alpha:1.0]];
    }
    
    NSString *event = [data objectForKey:@"event"];
    eventLabel.text = event;
    
    NSString *period = [data objectForKey:@"period"];
    periodLabel.text = period;
    
    NSString *survey = [data objectForKey:@"survey"];
    surveyLabel.text = survey;
    
    NSString *actual = [data objectForKey:@"actual"];
    if ([actual containsString:@"-"]) {
        actualLabel.textColor = [UIColor redColor];
    } else {
        actualLabel.textColor = [UIColor colorWithRed:24.0/255.0 green:130.0/255.0 blue:0.0 alpha:1.0];
    }
    actualLabel.text = actual;
    
    NSString *prior = [data objectForKey:@"prior"];
    proirLabel.text = prior;
    
    NSString *revised = [data objectForKey:@"revised"];
    revisedLabel.text = revised;
}

@end
